package com.puman.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.core.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.puman.bean.Auction;
import com.puman.bean.Competitive;
import com.puman.bean.CompetitiveView;
import com.puman.service.CompetitiveService;
@Results({@Result(name="list",location="/jsp/competitive/competitive_list.jsp"),
	@Result(name="addUI",location="/jsp/competitive/competitive_addUI.jsp"),
	@Result(name="getCities",location="/jsp/competitive/competitive_addUI.jsp"),
	@Result(name="getCounties",location="/jsp/competitive/competitive_addUI.jsp"),
	@Result(name="updateUI",location="/jsp/competitive/competitive_updateUI.jsp"),
	@Result(name="delete",location="/jsp/competitive/competitive_list.jsp"),
	@Result(name="auctionList",location="/jsp/competitive/auction_list.jsp"),
	@Result(name="relist",type="redirect",location="/competitive!list.do"),
})
public class CompetitiveAction extends BaseAction implements ModelDriven<Competitive> {

	private static final long serialVersionUID = -8747258280930680975L;

	@Resource
	private CompetitiveService competitiveService;
	private Integer totalCount;
	private List<Competitive> list;
	private List<Auction> auctionList;
	private Competitive model = new Competitive();
	private Integer supplier_id = 9;
	@Override
	public Competitive getModel() {
		return model;
	}
	
	public String list(){
		LOG.info("list...");
		list = competitiveService.getList(supplier_id);
		totalCount = list.size();
		return "list";
	}
	
	public String updateUI(){
		Integer id = Integer.valueOf(getParameter("id"));
		model = competitiveService.get(id);
		return "updateUI";
	}
	
	public String update(){
		Competitive competitive = competitiveService.get(model.getID());
		competitive.setPRICE_TYPE(model.getPRICE_TYPE());
		if(model.getPRICE_TYPE() == 0){
			competitive.setPRICE(model.getPRICE());
		}else{
			competitive.setPRICE1(model.getPRICE1());
		}
		competitive.setNOTE(model.getNOTE());
		competitive.setMODIFY_DATE(new Date());
		competitive.setMODIFY_USER_ID(1);
		competitive.setMODIFY_USER_NAME("admin");
		competitiveService.update(competitive);
		return "relist";
	}
	
	public String addUI(){
		Integer auction_id = Integer.valueOf(getParameter("id"));
		model.setAUCTION_ID(auction_id);
		return "addUI";
	}
	
	public String add(){
		model.setSUPPLIER_ID(supplier_id);
		model.setCOMPETITIVETIME(new Date());
		model.setSTATUS(0);
		model.setCREATE_DATE(new Date());
		model.setCREATE_USER_ID(1);
		model.setCREATE_USER_NAME("admin");
		model.setMODIFY_DATE(new Date());
		model.setMODIFY_USER_ID(1);
		model.setMODIFY_USER_NAME("admin");
		competitiveService.save(model);
		return "relist";
	}
	
	public String auctionList(){
		auctionList = competitiveService.getAuctionList(supplier_id);
		totalCount = auctionList.size();
		return "auctionList";
	}
	
	public String queryByName(){
		String name = getParameter("queryName");
		Integer supplier_id = 9;
		list = competitiveService.getListLike(supplier_id,name);
		totalCount  = list.size();
		return "list";
	}
	
	public Integer getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public List<Competitive> getList() {
		return list;
	}

	public void setList(List<Competitive> list) {
		this.list = list;
	}

	public void setAuctionList(List<Auction> auctionList) {
		this.auctionList = auctionList;
	}

	public List<Auction> getAuctionList() {
		return auctionList;
	}
}
